package com.yuntsg.ruiijn.controller.search;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuntsg.ruiijn.database_genrepository.entity.FileDetailsGenrepository;
import com.yuntsg.ruiijn.database_genrepository.entity.FileRecordGenrepository;
import com.yuntsg.ruiijn.database_genrepository.service.FileDetailsGenrepositoryService;
import com.yuntsg.ruiijn.database_genrepository.service.FileRecordGenrepositoryService;
import com.yuntsg.ruiijn.database_genrepository.service.PdfGenrepositoryService;
import com.yuntsg.ruiijn.databse_search.service.SearchSelfService;
import common.bean.PubSearchRequestVo;
import common.bean.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("genrepository")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@SuppressWarnings("all")
@Slf4j
public class CenRepositoryController {


    private final PdfGenrepositoryService pdfService;

    private final FileRecordGenrepositoryService fileRecordService;

    private final FileDetailsGenrepositoryService fileDetailService;

    private final SearchSelfService searchService;


    /**
     * 文件上传
     *
     * @param file   文件
     * @param title  标题
     * @param author 作者
     * @param pmut   pmut
     * @return
     */
    @PostMapping("upload")
    @ResponseBody
    public R downloadpdf(@RequestParam(required = false) MultipartFile file, @RequestParam("title") String title, @RequestParam("author") String author, @RequestParam("pmut") String pmut) {
        try {
            return R.ok().put("data", pdfService.upload(file, title, author, pmut));
        } catch (IOException e) {
            return R.error("上传失败");
        }
    }

    /**
     * 文件提取
     *
     * @param rid rid
     * @return
     */
    @GetMapping("extract/{rid}")
    @ResponseBody
    public R extract(@PathVariable Integer rid) {
        try {
            return R.ok().put("data", pdfService.extract(rid));
        } catch (IOException e) {
            return R.error("提取失败");
        }
    }

    /**
     * 文件列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("records")
    @ResponseBody
    public R records(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        Page<FileRecordGenrepository> p = fileRecordService.page(new Page<>(page, size), Wrappers.<FileRecordGenrepository>lambdaQuery().orderByDesc(FileRecordGenrepository::getId));
        List<FileRecordGenrepository> records = p.getRecords();
        records.forEach(re -> re.setSyNum(fileDetailService.count(Wrappers.<FileDetailsGenrepository>lambdaQuery().eq(FileDetailsGenrepository::getRid, re.getId()).eq(FileDetailsGenrepository::getSyncStatus, 2))));
        if (p.getTotal() > 0) {
            return R.ok().put("data", records).put("total", p.getTotal());
        } else {
            return R.error("没有数据");
        }
    }

//    @GetMapping("fetchInfo/{pmid}")
//    @ResponseBody
//    public R fetchInfoWithPM(@PathVariable("pmid") String pmid) throws IOException {
//        return R.ok().put("data", searchService.fetchInfoWithPM(pmid));
//    }


    /**
     * 小图列表
     *
     * @param rid
     * @param fid
     * @param type
     * @return
     */
    @GetMapping("picList/{rid}/{fid}/{type}")
    @ResponseBody
    public R picList(@PathVariable("rid") Integer rid, @PathVariable("fid") Integer fid, @PathVariable("type") Integer type) {
        List<FileDetailsGenrepository> fileDetailsList = fileDetailService.picList(rid, fid, type).stream().map(s -> s.setPath("oss/" + s.getPath())).collect(Collectors.toList());
        if (type == 2) {
            fileDetailsList.forEach(s -> {
                Integer id = s.getId();
                Long not = fileDetailService.count(Wrappers.<FileDetailsGenrepository>lambdaQuery().eq(FileDetailsGenrepository::getFId, id).ne(FileDetailsGenrepository::getTpye, 2).eq(FileDetailsGenrepository::getDeleteFlag, 0).in(FileDetailsGenrepository::getSyncStatus, 0, 1));
                Long yes = fileDetailService.count(Wrappers.<FileDetailsGenrepository>lambdaQuery().eq(FileDetailsGenrepository::getFId, id).ne(FileDetailsGenrepository::getTpye, 2).eq(FileDetailsGenrepository::getDeleteFlag, 0).eq(FileDetailsGenrepository::getSyncStatus, 2));
                s.setSyncStr(not + "/" + yes);

            });
        }
        return R.ok().put("data", fileDetailsList);
    }


    /**
     * 手动切割
     *
     * @param requestVo
     * @return
     */
    @PostMapping("pictureManually")
    @ResponseBody
    public R pictureManually(@RequestBody PubSearchRequestVo requestVo) {
        return fileDetailService.pictureManually(requestVo);
    }

    /**
     * 添加到待同步列表
     *
     * @param id
     * @return
     */
    @GetMapping("putpendinglist/{code}/{fid}")
    @ResponseBody
    public R putpendinglist(@PathVariable("code") Integer code, @PathVariable("fid") Integer fid, @RequestParam(value = "ids[]") String[] ids) {
        fileDetailService.update(Wrappers.<FileDetailsGenrepository>lambdaUpdate().set(FileDetailsGenrepository::getSyncStatus, 1).in(FileDetailsGenrepository::getId, ids));
        return R.ok("操作成功");
    }


    /**
     * 已同步列表
     *
     * @param rid
     * @return
     */
    @GetMapping("pendinglist/{rid}/{type}")
    @ResponseBody
    public R pendinglist(@PathVariable("rid") Integer rid, @PathVariable("type") Integer type) {
        return R.ok().put("data", fileDetailService.pendinglist(rid, type).stream().map(s -> s.setPath("oss/" + s.getPath())).collect(Collectors.toList()));
    }


    /**
     * 同步数据
     *
     * @param rid
     * @return
     */
    @PostMapping("datasynchronization")
    @ResponseBody
    public R pendinglist(@RequestParam(value = "ids[]") String[] ids) {
        fileDetailService.putPendingList(ids);
        return R.ok().put("data", "操作成功");
    }


    /**
     * 删除数据
     */
    @PostMapping("del")
    @ResponseBody
    public R delete(@RequestParam(value = "ids[]") String[] ids) {
        fileDetailService.delete(ids);
        return R.ok().put("data", "操作成功");
    }


}
